//
//  ViewController.m
//  CoreDate
//
//  Created by dllo on 15/11/20.
//  Copyright (c) 2015年 蓝鸥3g学院. All rights reserved.
//

#import "ViewController.h"
#import "Student.h"
#import "ConeDataManager.h"

@interface ViewController ()

@property(nonatomic, strong)ConeDataManager *manager;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.manager = [ConeDataManager defaults];
    
    [self.manager applicationDocumentsDirectory];
    NSLog(@"=======%@ ", self.manager.applicationDocumentsDirectory);
    
}
- (IBAction)add:(id)sender {
    //创建实体描述
    //1.描述的实体
    //2.数据管理器
    
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Student" inManagedObjectContext:self.manager.managedObjectContext];
    
    //创建实体
    //1.实体描述
    //2.数据管理器
    Student *stu = [[Student alloc] initWithEntity:entity insertIntoManagedObjectContext:self.manager.managedObjectContext];
    stu.name = @"chengjing";
    stu.age = @1222;
    stu.sex = @"?";
    stu.number = @11;
    stu.hobby = @"吹🐂13";
    
    [self.manager saveContext];
    
}


//删除
- (IBAction)delete:(id)sender {
    
//    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Student"];
//    
//    NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@ == %@", @"sex", @"%@"], @"女"];
//    request.predicate = predicate;
//    
//    
//    //通过某个key给数组排序
//    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"sex" ascending:YES];
//    
//    [request setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
//    
//    NSError *error;
//    NSArray *array = [self.manager.managedObjectContext executeFetchRequest:request error:&error];

    
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Student" inManagedObjectContext:self.manager.managedObjectContext];
    [fetchRequest setEntity:entity];
    // Specify criteria for filtering which objects to fetch
    NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@ == %@", @"sex", @"%@"], @"?"];
    [fetchRequest setPredicate:predicate];
    // Specify how the fetched objects should be sorted
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"sex"
                                                                   ascending:YES];
    [fetchRequest setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
    
    NSError *error = nil;
    NSArray *fetchedObjects = [self.manager.managedObjectContext executeFetchRequest:fetchRequest error:&error];
    
    for (Student *stu in fetchedObjects) {
        [self.manager.managedObjectContext deleteObject:stu];
    }
    
    [self.manager saveContext];
}

//修改
- (IBAction)update:(id)sender {
    //获取查询数据请求,相当于数据库的查询语句
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Student"];
    
    //添加一个约束   谓词
    
    NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@ == %@", @"name", @"%@"], @"chengjing"];
    
    request.predicate = predicate;
    
    //通过某个key给数组排序
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    
    [request setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
    
    NSError *error;
    NSArray *array = [self.manager.managedObjectContext executeFetchRequest:request error:&error];

    Student *stu = [array lastObject];
    stu.sex = @"女";
    
    [self.manager saveContext];
    
    
}


//查找
- (IBAction)select:(id)sender {
    
    //获取查询数据请求,相当于数据库的查询语句
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Student"];
    
    //添加一个约束   谓词
    
    NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@ == %@", @"name", @"%@"], @"chengjing"];
    
    request.predicate = predicate;
    
    //通过某个key给数组排序
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    
    [request setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
    
    NSError *error;
    NSArray *array = [self.manager.managedObjectContext executeFetchRequest:request error:&error];
    NSLog(@"%ld", array.count);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
